<?php
use Workerman\Worker;
use Workerman\Connection\AsyncUdpConnection;
require_once dirname(__DIR__) . '/vendor/autoload.php';
$ws_worker = new Worker("websocket://0.0.0.0:2000");
$ws_worker->onWorkerStart = function ($ws_worker){
    $udp_connection = new AsyncUdpConnection('udp://127.0.0.1:9302');
    $udp_connection->onConnect = function($udp_connection){
        $udp_connection->send('hi');
    };
    $udp_connection->onMessage = function($udp_connection, $data){
        $_SESSION['sip_server'] = $udp_connection;
    };
    $udp_connection->connect();
};
$ws_worker->onMessage=function($connection,$msg){
    $call_id="799879999";
    $port="6278";
    if($msg=='on'){
        /*消息拼装*/
        $ml="INVITE sip:34020000001310000001@192.168.1.92:9300 SIP/2.0\r\nVia: SIP/2.0/UDP 192.168.1.80:9301;rport;branch=z9hG4bK14273066\r\nFrom: <sip:34020000002000000001@192.168.1.80:9301>;tag=539567811\r\nTo: <sip:34020000001310000001@192.168.1.92:9300>\r\nCall-ID: ".$call_id."\r\nCSeq: 1 Invite\r\nContent-Type: APPLICATION/SDP\r\nMax-Forwards: 70\r\nUser-Agent: eXosip/3.6.0\r\nContact:<sip:34020000002000000001@192.168.1.80:9301>\r\nSubject:34020000001310000001:015359105,34020000002000000001:015369105\r\nContent-Length: 240\r\n\r\nv=0\r\no=34020000002000000001 0 0 IN IP4 192.168.1.80\r\ns=Play\r\nc=IN IP4 192.168.1.80\r\nt=0 0\r\nm=video ".$port." RTP/AVP 96 98 97\r\na=recvonly\r\na=rtpmap:96 PS/90000\r\na=rtpmap:98 H264/90000\r\na=rtpmap:97 MPEG4/90000\r\ny=0100000001\r\nf=";
        /*消息转发*/
        $_SESSION['sip_server']->send($ml);
        sleep(1);
        /*消息拼装*/
        $ml="ACK sip:34020000001310000001@192.168.1.92 SIP/2.0\r\nVia: SIP/2.0/UDP 192.168.1.80:9301;rport;branch=z9hG4bK14273066\r\nFrom: <sip:34020000001310000001@192.168.1.80:9301>;tag=539567811\r\nTo: <sip:34020000002000000001@192.168.1.92>;tag=139512741\r\nCall-ID: ".$call_id."\r\nCSeq: 1 ACK\r\nMax-Forwards: 70\r\nUser-Agent: eXosip/3.6.0\r\nContent-Length: 0\r\n\r\n";
        /*消息转发*/
        $_SESSION['sip_server']->send($ml);
    }elseif ($msg=='off'){
        /*消息拼装*/
        $ml="BYE sip:34020000001310000001@192.168.1.92 SIP/2.0\r\nVia: SIP/2.0/UDP 192.168.1.80:9301;rport;branch=z9hG4bK14273066\r\nFrom: <sip:34020000002000000001@192.168.1.80:9301>;tag=539567811\r\nTo: <sip:34020000001310000001@192.168.1.92>;tag=139512741\r\nCall-ID: ".$call_id."\r\nCSeq: 2 BYE ACK\r\nMax-Forwards: 70\r\nUser-Agent: eXosip/3.6.0\r\nContent-Length: 0\r\n\r\n";
        /*消息转发*/
        $res = $_SESSION['sip_server']->send($ml);
    }elseif ($msg=='right'){
        $ml="MESSAGE sip:34020000002000000001@192.168.1.80:9301 SIP/2.0\r\nVia: SIP/2.0/UDP 192.168.1.92:9300;rport;branch=z9hG4bK45002270\r\nFrom: <sip:34020000001310000001@192.168.1.92>;tag=113001187\r\nTo: <sip:34020000002000000001@192.168.1.80:9301>\r\nCall-ID: 1517082125\r\nCSeq: 20 MESSAGE\r\nContent-Type: Application/MANSCDP+xml\r\nMax-Forwards: 70\r\nUser-Agent: eXosip/3.6.0\r\nContent-Length:   260\r\n\r\n<? xml version=\"1.0\"?>\r\n<Control>\r\n<CmdType>DeviceControl</CmdType>\r\n<SN>11</SN>\r\n<DeviceID>34020000001310000001</DeviceID>\r\n<PTZCmd> A50F01021F0000D6</PTZCmd>\r\n<Info>\r\n<ControlPriority>5</ControlPriority>\r\n</Info>\r\n</Control>";
        $_SESSION['sip_server']->send($ml);
    }elseif ($msg=='stop'){
        $ml="MESSAGE sip:34020000002000000001@192.168.1.80:9301 SIP/2.0\r\nVia: SIP/2.0/UDP 192.168.1.92:9300;rport;branch=z9hG4bK45002270\r\nFrom: <sip:34020000001310000001@192.168.1.92>;tag=113001187\r\nTo: <sip:34020000002000000001@192.168.1.80:9301>\r\nCall-ID: 1517082125\r\nCSeq: 20 MESSAGE\r\nContent-Type: Application/MANSCDP+xml\r\nMax-Forwards: 70\r\nUser-Agent: eXosip/3.6.0\r\nContent-Length:   260\r\n\r\n<? xml version=\"1.0\"?>\r\n<Control>\r\n<CmdType>DeviceControl</CmdType>\r\n<SN>11</SN>\r\n<DeviceID>34020000001310000001</DeviceID>\r\n<PTZCmd>A50F010*******B5</PTZCmd>\r\n<Info>\r\n<ControlPriority>5</ControlPriority>\r\n</Info>\r\n</Control>";
        $_SESSION['sip_server']->send($ml);
    }
};

if (!defined('GLOBAL_START')) {
    \Workerman\Worker::runAll();
}